package cn.hs.ph.servlet;

import cn.hs.ph.dao.StationDao;
import cn.hs.ph.dao.impl.StationDaoImpl;
import cn.hs.ph.entity.Station;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/station")
public class StationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        String action = req.getParameter("action");
        Map<String,String> map = new HashMap<>();
        StationDao stationDao = new StationDaoImpl();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if ("select".equals(action)){
            String courierStationName = req.getParameter("courierStationName");
            String communityName = req.getParameter("communityName");
            String courierStationStatus = req.getParameter("courierStationStatus");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            map.put("courierStationName",courierStationName);
            map.put("communityName",communityName);
            map.put("courierStationStatus",courierStationStatus);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);

            List<Station> list = stationDao.selectAll(map);
            int total = stationDao.selectCount(map);
            Map<Object,Object> resultmap = new HashMap<>();

            resultmap.put("list",list);
            resultmap.put("total",total);
            Result<Station> result = Result.success(resultmap);
            resp.getWriter().write(JSON.toJSONString(result));
        }
        else if("update".equals(action)){
            int courierStationCommunityId = 0;
            if(req.getParameter("courierStationCommunityId")!=null){
                courierStationCommunityId = Integer.parseInt(req.getParameter("courierStationCommunityId"));
            }
            int update = stationDao.update(new Station(
                    Integer.parseInt(req.getParameter("courierStationId")),
                    req.getParameter("courierStationName"),
                    req.getParameter("courierStationImage"),
                    req.getParameter("courierStationProvince"),
                    req.getParameter("courierStationCity"),
                    req.getParameter("courierStationDistrict"),
                    req.getParameter("courierStationAddress"),
                    format.format(new Date()),
                    courierStationCommunityId,
                    req.getParameter("courierStationCommunityName"),
                    Integer.parseInt(req.getParameter("courierStationOrderCount")),
                    Integer.parseInt(req.getParameter("courierStationSort")),
                    Integer.parseInt(req.getParameter("courierStationStatus"))
            ));
            if(update != 0){
                Result<Station> result = Result.success("修改成功");
                resp.getWriter().write(JSON.toJSONString(result));
            }else {
                Result<Station> result = Result.error("修改失败");
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }
        else if("insert".equals(action)){
            int insert = stationDao.insert(new Station(
                    req.getParameter("courierStationName"),
                    req.getParameter("courierStationImage"),
                    req.getParameter("courierStationProvince"),
                    req.getParameter("courierStationCity"),
                    req.getParameter("courierStationDistrict"),
                    req.getParameter("courierStationAddress"),
                    Integer.parseInt(req.getParameter("courierStationCommunityId")),
                    req.getParameter("courierStationCommunityName"),
                    0,
                    0,
                    0
            ));
            if(insert != 0){
                Result<Station> result = Result.success("添加成功");
                resp.getWriter().write(JSON.toJSONString(result));
            }else {
                Result<Station> result = Result.error("添加失败");
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }
        else if("delete".equals(action)){
            int Id = stationDao.delete(Integer.parseInt(req.getParameter("courierStationId")));
            if(Id != 0){
                Result<Station> result = Result.success("删除成功");
                resp.getWriter().write(JSON.toJSONString(result));
            }else {
                Result<Station> result = Result.error("删除失败");
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }
    }
}
